package com.wcl.nullmusic.ui.fragment;

import android.os.Handler;
import android.util.Log;
import android.view.View;

import com.wcl.nullmusic.R;
import com.wcl.nullmusic.base.BaseFragment;
import com.wcl.nullmusic.mode.PlayMusic;
import com.wcl.nullmusic.ui.service.NullMusicService;
import com.wcl.nullmusic.utils.LyricGesture;
import com.wcl.nullmusic.view.LyricView2;

import de.greenrobot.event.EventBus;

/**
 * Created by dllo on 16/1/25.
 */
public class LrcFragment extends BaseFragment {


    private LyricView2 lyricView;
    private Thread thread = null;
    private boolean running = true;
    private float ldriftx;
    private float ldrifty;
    private int currentPosition;
    private boolean isPlaying=true;
    private PlayMusic playMusic;

    @Override
    protected int getLayout() {
        return R.layout.fragment_lrc;
    }

    @Override
    protected void getVaule() {

    }

    @Override
    protected void initView(View view) {
        EventBus.getDefault().register(this);
        lyricView = (LyricView2) view.findViewById(R.id.lrc_lrc);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void listener() {
        lyricView.setLongClickable(true);
        lyricView.setOnTouchListener(new LyricGesture(this));
    }

    @Override
    public void onResume() {
        super.onResume();
        //���߳����ڻ�ȡ��ǰ����ʱ����
        if (thread == null) {
            thread = new Thread(new UIUpdateThread());
            thread.start();
        }
        if (!thread.isAlive()) {
            System.out.println("~~~~~!thread.isAlive()");
            running = true;
            thread.interrupt();
            thread = null;
            thread = new Thread(new UIUpdateThread());
            thread.start();
        }
    }

    class UIUpdateThread implements Runnable {
        @Override
        public void run() {
            while (running) {
                if (!isPlaying) {
                    running = false;
                    thread.interrupt();
                    thread = null;
                }
              //  int time = currentPosition;
                int sleeptime = 100;
                //float drift =
                lyricView.updateindex(currentPosition);
                //time += sleeptime;
                mHandler.post(mUpdateResults);
                //System.out.println("mp.isPlaying()~~~"+time+"!!"+sleeptime+"!!");
                if (sleeptime == -1)
                    return;
                try {
                    Thread.sleep(sleeptime);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    Handler mHandler = new Handler();
    Runnable mUpdateResults = new Runnable() {

        @Override
        public void run() {
            Log.i(",.","进入handler刷新视图");
            lyricView.invalidate();//������ͼ
        }
    };

    public void slidestart() {
        if (thread != null) {
            if (thread.isAlive()) {
                lyricView.showprogress = true;
                running = false;
                thread.interrupt();
                thread = null;
            }
        }
    }

    public boolean updatelab(float dx, float dy, boolean toggle) {
        if (toggle) {
            ldriftx = dx + ldriftx;
            ldrifty = dy + ldrifty;
        } else {
            if (Math.abs(ldriftx) < Math.abs(ldrifty)) {
                return true;
            }
            ldriftx = 0;
            ldrifty = 0;
        }
        return false;
    }

    public void updateprogress(float dx, float dy) {
        lyricView.driftx = dx + lyricView.driftx;
        lyricView.drifty = dy + lyricView.drifty;
        lyricView.invalidate();//������ͼ
    }

    public void updateplayer() {
        lyricView.showprogress = false;
//		if(Math.abs(lyricView.driftx)<Math.abs(lyricView.drifty)){
        lyricView.index = lyricView.index + lyricView.temp;
        lyricView.driftx = 0;
        lyricView.drifty = 0;
        if (lyricView.repair()) {

            playMusic = new PlayMusic();
            playMusic.setState(NullMusicService.CHANGE_MUSIC_CURRENTPOSITION);
            playMusic.setCurrentPosition(Integer.parseInt(lyricView.time[lyricView.index - 1]));
            EventBus.getDefault().post(playMusic);


            //   mp.seekTo(Integer.parseInt(lyricView.time[lyricView.index - 1]));
        } else// mp.seekTo(0);
        {
            playMusic = new PlayMusic();
            playMusic.setState(NullMusicService.CHANGE_MUSIC_CURRENTPOSITION);
            playMusic.setCurrentPosition(0);
            EventBus.getDefault().post(playMusic);
        }


        //�����߳�
        running = true;
        thread = new Thread(new UIUpdateThread());
        thread.start();
        //}
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        if (thread != null) {
            running = false;
            thread.interrupt();
        }
        super.onDestroy();
    }

    public void onEvent(PlayMusic playMusic) {
       // Log.i("i", "有接到的信息  lrc");
        int state = playMusic.getState();
        switch (state) {

            case NullMusicService.SEND_MUSIC_CURRENTPOSITION:

                currentPosition = playMusic.getCurrentPosition();
                Log.i("pp", "有接到播放的当前位置 "+currentPosition);
                break;
            case NullMusicService.SEND_MUSIC_ISPLAYING:

            //    isPlaying = playMusic.isCurrentIsPlaying();
                Log.i("io", "有接到的信息  isplaying 播放状态"+isPlaying);
                break;
//                if (isPlaying) {
//                    EventBus.getDefault().post(NullMusicService.OBTAIN_MUSIC_ALLINFO);
//
//        }
        }
    }

}
